/*
 * Main.fx
 *
 * Created on 08.05.2010, 15:51:25
 */

package javafxapplication3;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.ext.swing.SwingTextField;
import javafx.ext.swing.SwingLabel;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import java.lang.*;
import javafx.scene.control.Slider;
import javafx.scene.control.ProgressBar;
import javafx.scene.shape.Polyline;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.control.ListView;

var present:String[];
var presentUser:String[];
var counter:Integer; //
var counterUser:Integer; //

function Check(str:String):Integer
{   //System.out.println("Check");
    for(nu in [0..100])
    {   if(present[nu] == str)
        {   //System.out.println("1");
            return 1;
        }
    }
    //System.out.println("0");
    return 0;
}
function CheckUser(str:String):Integer
{   //System.out.println("Check");
    for(nu in [0..100])
    {   if(presentUser[nu] == str)
        {   //System.out.println("1");
            return 1;
        }
    }
    //System.out.println("0");
    return 0;
}



function Calculate(r:Integer,g:Integer,b:Integer)
{    for(n in [0..counter])
            present[n]="";
    var str:String;
    var porR:Integer;
    var porB:Integer;
    var porG:Integer;
    var por:Integer;
    var R:Integer;
    var B:Integer;
    var G:Integer;
    var S:String;
    R=r;B=b;G=g;
    counter=1;
    if(r>0) porR=1
    else porR=0;
    if(b>0) porB=1
    else porB=0;
    if(g>0) porG=1
    else porG=0;
    if(G+R+B>0) por=1
    else { 
    por=0;
    return 0;
    }
    var time:Integer;// Отвечает что мы сейчас добавляем R G или B
    time=1;
    var num:Integer;
    num=0;
    //por=0;
    porR=0;
    porG=0;
    porB=0;
    while(true)
    { System.out.println("R={R} G={G} B={B}");
      if(R+B+G>=por)
      {
        //System.out.println("+");
         //System.out.println("time={time}");
         if(time==1)
      {     System.out.println("if (time==1) true");
            //System.out.println("R={R} porR={porR}");
            if(R-porR>0)
            {   System.out.println("if (R={R}-porR={porR} > 0) true");
                //System.out.println("num+1={num+1} por={por}");
                //System.out.println("if (R-porR>0) true");
                if(num+1==por)
                 {   System.out.println("if (num+1={num+1} == por={por}) true");
                        S=str;
                        S+="R";
                        //System.out.println("                                   Check={Check(S)}");
                        if(Check(S)==1)
                        {   System.out.println("if (Check(S={S})={Check(S)} == 1) true");
                            time++;
                            continue;
                        }
                        else
                        {   System.out.println("if (Check(S={S})={Check(S)} == 1) false");
                            porR++;
                            num=0;
                            System.out.println("Запись");
                            present[counter]=S;
                            System.out.println("S={S}");
                            System.out.println("present[{counter}]={present[counter]}");
                            counter++;
                            R-=porR;
                            B-=porB;
                            G-=porG;
                            porR=0;
                            porG=0;
                            porB=0;
                            str="";
                            //time++;
                        }
                    }
            else
            {   System.out.println("if (num+1={num+1} == por={por}) false");
                str+="R";
                System.out.println("+R str={str}");
                porR++;
                num++;
                continue;
            }
        }
        else
        { System.out.println("if (R={R}-porR={porR} > 0) false");
          time++;
          continue;
        }
      }
      else
      { if(time==2)
        { System.out.println("if (time==2) true");
            if(G-porG>0)
            {   System.out.println("if (G={G}-porG={porG} > 0) true");
                if(num+1==por)
                {   System.out.println("if (num+1={num+1} == por={por}) true");
                    S=str;
                    S+="G";
                    //System.out.println('S');
                    //System.out.println(S);
                    //System.out.println(S);
        
                    if(Check(S)==1)
                    {   System.out.println("if (Check(S={S})={Check(S)} == 1) true");
                        time++;
                        continue;
                    }
                    else
                    {   System.out.println("if (Check(S={S})={Check(S)} == 1) false");;
                        porG++;
                        num=0;
                        System.out.println("Запись");
                        System.out.println("S={S}");
                        present[counter]=S;
                        System.out.println("present[{counter}]={present[counter]}");
                        counter++;
                        R-=porR;
                        B-=porB;
                        G-=porG;
                        porR=0;
                        porG=0;
                        porB=0;
                        str="";
                        //time++;
                    }
                }
                else
                {   System.out.println("if (num+1={num+1} == por={por}) false");
                    str+="G";
                    System.out.println("+G str={str}");
                    porG++;
                    num++;
                    continue;
                }
            }
            else
            { System.out.println("if (G={G}-porG={porG} > 0) false");
              time++;
              continue;
            }
        }
        else//time=3
        {  System.out.println("if (time==3) true");
            if(B-porB>0)
            {   System.out.println("if (B={B}-porB={porB} > 0) true");
                if(num+1==por)
                {   System.out.println("if (num+1={num+1} == por={por}) true");
                    S=str;
                    S+="B";
                    if(Check(S)==1)
                    {   System.out.println("if (Check(S={S})={Check(S)} == 1) true");
                        time=1;
                        por++;
                        num=0;
                        porR=0;
                        porG=0;
                        porB=0;
                        str="";
                        continue;
                    }
                    else
                    {  System.out.println("if (Check(S={S})={Check(S)} == 1) false");
                        porB++;
                        num=0;
                        System.out.println("Запись");
                        present[counter]=S;
                        System.out.println("S={S}");
                        System.out.println("present[{counter}]={present[counter]}");
                        counter++;
                        R-=porR;
                        B-=porB;
                        G-=porG;
                        porR=0;
                        porG=0;
                        porB=0;
                        str="";
                        //time=1;
                    }
                }
                else
                {   System.out.println("if (num+1={num+1} == por={por}) false");
                    str+="B";
                    System.out.println("+B str={str}");
                    porB++;
                    num++;
                    continue;
                }
            }
            else
            { System.out.println("if (B={B}-porB={porB} > 0) false");
              time=1;
              por++;
              porR=0;
              porG=0;
              porB=0;
              num=0;
              str="";
              continue;
            }
        }
      }
      }
      else
        break;
    }
    return 1;
}

/**
 * @author Никитин Димитрий Георгиевич
 */
var Rg:Integer;
var Gg:Integer;
var Bg:Integer;
var Rc:Integer;
var Gc:Integer;
var Bc:Integer;
var str:String;
//var Result:Integer;
def RTextBox = SwingTextField { columns: 10
	text: "0"
	editable: true }
def GTextBox = SwingTextField { columns: 10
	text: "0"
	editable: true }
def BTextBox = SwingTextField { columns: 10
	text: "0"
	editable: true }
def LabelResult =  Label {text: "Результат"}
def LabelR = Label {text: "0"}
def LabelG = Label {text: "0"}
def LabelB = Label {text: "0"}
def AddButton = Button {
	text: "Добавить в набор"
	action: function() {
                str="";
                for(n in [1..Rc])
                    str+="R";
                for(n in [1..Gc])
                    str+="G";
                for(n in [1..Bc])
                    str+="B";
                //System.out.println("str={str} Rc={Rc} Gc={Gc} Bc={Bc} Rg={Rg} Gg={Gg} Bg={Bg}");
                if(CheckUser(str)==1)//Вывод сообщения о том,что уже есть такой в наборе.
                {  Rg+=Rc;
                   Rc=0;
                   Gg+=Gc;
                   Gc=0;
                   Bg+=Bc;
                   Bc=0;
                   RTextBox.text=String.valueOf(Rg);
                   LabelR.text=String.valueOf(Rc);
                   GTextBox.text=String.valueOf(Gg);
                   LabelG.text=String.valueOf(Gc);
                   BTextBox.text=String.valueOf(Bg);
                   LabelB.text=String.valueOf(Bc);
                   Stage
                   {    title : "Message"
                        scene: Scene
                               {    width: 200
                                    height: 200
                                    content:
                                    [   Text
                                        {   font : Font{size: 12}
                                            x: 10, y: 30
                                            content: "Такое уже существует в наборе!"
                                        }
                                    ]
                                }
                    }

                }
                else
                {   presentUser[counterUser]=str;
                    counterUser++; 
                    System.out.println("Добавлено {str}");
                    ListViewResult.items[counterUser]=str;
                    counterUser++;
                        /*Rectangle
                        {    x: 350, y: 500+n*50
                            width: 40, height: 40
                            fill: Color.RED
                        }*/

                   // for(n in [1..Gc])
                    //    str+="G";
                    //for(n in [1..Bc])
                    //    str+="B";
                    Rc=0;
                    Gc=0;
                    Bc=0;
                    RTextBox.text=String.valueOf(Rg);
                    LabelR.text=String.valueOf(Rc);
                    GTextBox.text=String.valueOf(Gg);
                    LabelG.text=String.valueOf(Gc);
                    BTextBox.text=String.valueOf(Bg);
                    LabelB.text=String.valueOf(Bc);

                }

	}
}
def incR = Button {
	text: "+Красный"
	action: function() {
                if(Rg>0)
                {   Rg--;
                    Rc++;
                    RTextBox.text=String.valueOf(Rg);
                    LabelR.text=String.valueOf(Rc);
                }
	}
}
def decR = Button {
	text: "-Красный"
	action: function() {
                if(Rc>0)
                {   Rc--;
                    Rg++;
                    RTextBox.text=String.valueOf(Rg);
                    LabelR.text=String.valueOf(Rc);
                }
	}
}
def incG = Button {
	text: "+Зеленый"
	action: function() {
                if(Gg>0)
                {   Gg--;
                    Gc++;
                    GTextBox.text=String.valueOf(Gg);
                    LabelG.text=String.valueOf(Gc);
                }
	}
}
def decG = Button {
	text: "-Зеленый"
	action: function() {
                if(Gc>0)
                {   Gc--;
                    Gg++;
                    GTextBox.text=String.valueOf(Gg);
                    LabelG.text=String.valueOf(Gc);
                }
	}
}
def incB = Button {
	text: "+Синий"
	action: function() {
                if(Bg>0)
                {   Bg--;
                    Bc++;
                    BTextBox.text=String.valueOf(Bg);
                    LabelB.text=String.valueOf(Bc);
                }
	}
}
def decB = Button {
	text: "-Синий"
	action: function() {
                if(Bc>0)
                {   Bc--;
                    Bg++;
                    BTextBox.text=String.valueOf(Bg);
                    LabelB.text=String.valueOf(Bc);
                }
	}
}
def ListViewResult = ListView {
	items: []
        }
def ListViewResult2 = ListView {
	items: []
        }
def ButtonShow = Button {
	text: "Сравнить"
	action: function() {
        for(n in [1..counter])
            ListViewResult2.items[n]=present[n];
        }
}

var SC = Scene {
        width: 800
        height: 800
        content: [
            VBox  {translateX: 10 translateY: 10 spacing: 20
                content:
                [
                    HBox {spacing: 5
                    content: [SwingLabel {
	text: "Красные"
}

         RTextBox

                            ]
}
HBox {spacing: 5
	content: [
                SwingLabel {
	text: "Синие"
}
       GTextBox
                ]


}
HBox { spacing: 5
	content: [
        SwingLabel {
	text: "Зеленые"
}
       BTextBox
       ]
}
HBox {
	content: [Button {
	text: "Начать"

	action: function() {

        Rg=Integer.parseInt(RTextBox.text);
        Gg=Integer.parseInt(GTextBox.text);
        Bg=Integer.parseInt(BTextBox.text);
        counterUser=0;
        Rc=0;Gc=0;Bc=0;
        Calculate(Rg,Gg,Bg);
        //LabelResult.text=present[1];
        System.out.println("Началось");
        //System.out.println("                                   Check={Check("R")}");
        for(n in [0..counter])
        {    System.out.println(present[n]);

        }
        System.out.println("Закончилось");
	}
}
]
}
HBox
{content:
    [   VBox
        {   content:
            [   HBox
                {   content:
                    [   incR,
                        decR,
                        LabelR
                            ]
                }

            ]
        }

    ]
}
HBox
{content:
    [   VBox
        {   content:
            [   HBox
                {   content:
                    [   incG,
                        decG,
                        LabelG
                            ]
                }

            ]
        }

    ]
}
HBox
{content:
    [   VBox
        {   content:
            [   HBox
                {   content:
                    [   incB,
                        decB,
                        LabelB
                            ]
                }

            ]
        }

    ]
}
HBox {
	content: [AddButton]
}
HBox
{content:
    [   VBox
        {   content:
            [   HBox
                {   content:
                    [   ListViewResult,
                        ListViewResult2
                            ]
                }

            ]
        }

    ]
}
HBox {
	content: [ButtonShow]
}
]
}
]
}
Stage {
    title: "Задача 'Подарки'"
    scene: SC
}